/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.DichVu;

import BUS.CTPhieuDichVu_BUS;
import BUS.DichVu_BUS;
import BUS.KhachHang_BUS;
import BUS.PhieuDichVu_BUS;
import DTO.CTPhieuDichVu_DTO;
import DTO.KhachHang_DTO;
import DTO.PhieuDichVu_DTO;
import GUI.MyTableModel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr
 */
public class JIFDatDichVu extends javax.swing.JInternalFrame {
    DefaultComboBoxModel cbbModel_dv;
    DefaultTableModel tbModel;
    ArrayList<KhachHang_DTO> ds_dv = new ArrayList<>();
    Vector tableRecords_dv = new Vector();
    Vector tableTitle_dv = new Vector();
    Float thanhTien_dv;
    /**
     * Creates new form JIFDatDichVu
     */
    public JIFDatDichVu() {
        initComponents();
        this.setSize(880, 560);
        showCbbMaKhach();
        showBangDichVu();
        tableTitle_dv.add("Mã Dịch Vụ");
        tableTitle_dv.add("Tên Dịch Vụ");
        tableTitle_dv.add("Đơn Giá");
        tablePhieuDichVu.setModel(new MyTableModel(tableRecords_dv, tableTitle_dv));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbbMaKhach = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfTenKhach = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfSoDienThoai = new javax.swing.JTextField();
        tfThanhTien = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        dpNgayDat = new org.jdesktop.swingx.JXDatePicker();
        jLabel5 = new javax.swing.JLabel();
        btXacNhan = new javax.swing.JButton();
        btHuy = new javax.swing.JButton();
        btThoat = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableDichVu = new org.jdesktop.swingx.JXTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablePhieuDichVu = new org.jdesktop.swingx.JXTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("ĐẶT DỊCH VỤ");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin Khách hàng"));
        jPanel1.setToolTipText("");
        jPanel1.setMinimumSize(new java.awt.Dimension(237, 110));
        jPanel1.setName("Thông tin Khách hàng"); // NOI18N

        cbbMaKhach.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbMaKhachItemStateChanged(evt);
            }
        });

        jLabel1.setText("Mã Khách");

        jLabel2.setText("Tên Khách Hàng");

        jLabel3.setText("Số Điện Thoại");

        tfThanhTien.setEditable(false);
        tfThanhTien.setBackground(new java.awt.Color(204, 204, 204));
        tfThanhTien.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfThanhTien.setText("0");

        jLabel6.setText("Thành Tiền");

        dpNgayDat.setDate(new Date(System.currentTimeMillis()));
        dpNgayDat.setEditable(false);

        jLabel5.setText("Ngày Đặt");

        btXacNhan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/1371825243_Apply.png"))); // NOI18N
        btXacNhan.setText("Xác Nhận");
        btXacNhan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btXacNhanMouseEntered(evt);
            }
        });
        btXacNhan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXacNhanActionPerformed(evt);
            }
        });

        btHuy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Erase.png"))); // NOI18N
        btHuy.setText("Hủy Dịch Vụ");
        btHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btHuyActionPerformed(evt);
            }
        });

        btThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close.png"))); // NOI18N
        btThoat.setText("Thoát");
        btThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cbbMaKhach, 0, 148, Short.MAX_VALUE)
                    .addComponent(tfTenKhach)
                    .addComponent(tfSoDienThoai))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfThanhTien, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(dpNgayDat, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(43, 43, 43)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btXacNhan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btHuy, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbMaKhach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(dpNgayDat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(btXacNhan))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfThanhTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)
                        .addComponent(btHuy))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfTenKhach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfSoDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addComponent(btThoat))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dịch vụ"));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(327, 200));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(327, 200));
        jScrollPane1.setRequestFocusEnabled(false);

        tableDichVu.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableDichVu.setEditable(false);
        tableDichVu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableDichVuMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableDichVu);

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Dịch vụ đã chọn"));

        tablePhieuDichVu.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tablePhieuDichVu.setEditable(false);
        jScrollPane2.setViewportView(tablePhieuDichVu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbbMaKhachItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbMaKhachItemStateChanged
        try {
            // TODO add your handling code here:
            tfTenKhach.setText(KhachHang_BUS.layTenKHTheoMa(cbbMaKhach.getSelectedItem().toString()));
        } catch (Exception ex) {
            Logger.getLogger(JIFDatDichVu.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            // TODO add your handling code here:
            tfSoDienThoai.setText(KhachHang_BUS.laySoDTTheoMa(cbbMaKhach.getSelectedItem().toString()));
        } catch (Exception ex) {
            Logger.getLogger(JIFDatDichVu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_cbbMaKhachItemStateChanged

    private void btXacNhanMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btXacNhanMouseEntered
        // TODO add your handling code here:
        //btXacNhan.setBackground(Color.red);
    }//GEN-LAST:event_btXacNhanMouseEntered

    private void btXacNhanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXacNhanActionPerformed
        // TODO add your handling code here:
        String maKhach = cbbMaKhach.getSelectedItem().toString();
        String maPhieuDat = layMaPhieuDat();
        String thanhTien = tfThanhTien.getText();
        Date ngay = dpNgayDat.getDate();
        SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
        String ngayDat = format.format(ngay);
        if (tablePhieuDichVu.getRowCount() == 0) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa chọn dịch vụ!");
            return;
        }
        try {
            PhieuDichVu_DTO p = new PhieuDichVu_DTO();
            p.setMaKhach(maKhach);
            p.setMaPhieuDichVu(maPhieuDat);
            p.setTriGia(thanhTien);
            p.setNgayDat(ngayDat);
            PhieuDichVu_BUS.Insert(p);
            for (int i = 0; i < tablePhieuDichVu.getRowCount(); i++) {
                String maCTPhieuDichVu = maCTPhieuDichVu();
                CTPhieuDichVu_DTO ct = new CTPhieuDichVu_DTO();
                ct.setMaCTPhieuDichVu(maCTPhieuDichVu);
                ct.setMaPhieuDichVu(p.getMaPhieuDichVu());
                ct.setMaDichVu(tablePhieuDichVu.getValueAt(i, 0).toString());
                ct.setThanhTien(thanhTien);
                CTPhieuDichVu_BUS.Insert(ct);
            }
            JOptionPane.showMessageDialog(rootPane, "Đặt dịch vụ thành công");
            tableRecords_dv.clear();
            tablePhieuDichVu.setModel(new MyTableModel(tableRecords_dv, tableTitle_dv));
            tfThanhTien.setText("0");
        } catch (Exception ex) {
            Logger.getLogger(JIFDatDichVu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btXacNhanActionPerformed

    private void btHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btHuyActionPerformed
        // TODO add your handling code here:
        int row = tablePhieuDichVu.getSelectedRow();
        if(row==-1){
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa chọn dịch vụ!");
            return;
        }
        thanhTien_dv -= Float.parseFloat(tablePhieuDichVu.getValueAt(row, 2).toString());
        tfThanhTien.setText(thanhTien_dv.toString());
        tableRecords_dv.remove(row);
        tablePhieuDichVu.setModel(new MyTableModel(tableRecords_dv, tableTitle_dv));
    }//GEN-LAST:event_btHuyActionPerformed

    private void tableDichVuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableDichVuMouseClicked
        // TODO add your handling code here:
        int row = this.tableDichVu.getSelectedRow();
        for (int i = 0; i < tablePhieuDichVu.getRowCount(); i++) {
            if (tablePhieuDichVu.getValueAt(i, 0) == tableDichVu.getValueAt(row, 0)) {
                return;
            }
        }
        if(row <0 || row > tableDichVu.getRowCount()) return;
        Vector dichVu = new Vector();
        dichVu.add((String) this.tbModel.getValueAt(row, 0).toString());
        dichVu.add((String) this.tbModel.getValueAt(row, 1).toString());
        dichVu.add((String) this.tbModel.getValueAt(row, 2).toString());
        thanhTien_dv = Float.parseFloat(tfThanhTien.getText());
        thanhTien_dv += Float.parseFloat(tableDichVu.getValueAt(row, 2).toString());
        tfThanhTien.setText(thanhTien_dv.toString());
        tableRecords_dv.add(dichVu);
        tablePhieuDichVu.setModel(new MyTableModel(tableRecords_dv, tableTitle_dv));
    }//GEN-LAST:event_tableDichVuMouseClicked

    private void btThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btThoatActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btHuy;
    private javax.swing.JButton btThoat;
    private javax.swing.JButton btXacNhan;
    private javax.swing.JComboBox cbbMaKhach;
    private org.jdesktop.swingx.JXDatePicker dpNgayDat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private org.jdesktop.swingx.JXTable tableDichVu;
    private org.jdesktop.swingx.JXTable tablePhieuDichVu;
    private javax.swing.JTextField tfSoDienThoai;
    private javax.swing.JTextField tfTenKhach;
    private javax.swing.JTextField tfThanhTien;
    // End of variables declaration//GEN-END:variables

    private void showCbbMaKhach() {
        cbbModel_dv = (DefaultComboBoxModel) cbbMaKhach.getModel();
        try {
            ds_dv = KhachHang_BUS.layKhachDangThue();
        } catch (Exception e) {
        }

        for (KhachHang_DTO lp : ds_dv) {
            cbbModel_dv.addElement(lp.getMaKhachHang());
        }
    }

    private void showBangDichVu() {
        try {
            tbModel = DichVu_BUS.layBangDichVu();
            tableDichVu.setModel(tbModel);
        } catch (Exception e) {
        }
    }

    private String layMaPhieuDat() {
        String nextID = null;
        try {
            String lastID = PhieuDichVu_BUS.layMaCuoiCung();
            if (lastID != null) {
                nextID = PhieuDichVu_BUS.taoMaTuDong(lastID, 4);
            } else {
                nextID = PhieuDichVu_BUS.taoMaTuDong("MPDV0000", 4);
            }
            
        } catch (Exception e) {
        }
        return nextID;
    }

    private String maCTPhieuDichVu() throws Exception {
        String lastID = CTPhieuDichVu_BUS.layMaCuoiCung();
        String nextID = null;
        try {
            if (lastID != null) {
                nextID = CTPhieuDichVu_BUS.taoMaTuDong(lastID, 5);
            } else {
                nextID = CTPhieuDichVu_BUS.taoMaTuDong("MCTDV000000", 5);
            }
        } catch (Exception e) {
        }
        return nextID;
    }
}
